#include "StdAfx.h"
#include "ServerSocketList.h"

CServerSocketList::CServerSocketList(void)
{
}

CServerSocketList::~CServerSocketList(void)
{
}

void CServerSocketList::Push(SOCKET iSocket)
{
	_listSocket.push_back(iSocket);
	_listUser.resize(_listSocket.size());
}
int CServerSocketList::FindBySocket(SOCKET iSocket)
{
	for (int i = 0;i<_listSocket.size();i++)
	{
		if (_listSocket[i] == iSocket)
			return i;
	}
	return -1;
}
int CServerSocketList::FindByUserName(char* iUserName)
{
	for (int i = 0;i<_listUser.size();i++)
	{
		if (_listUser[i] != NULL)
		{
			if (strcmp(_listUser[i],iUserName) == 0)
				return i;
		}
	}
	return -1;
}

int CServerSocketList::Size()
{
	return _listSocket.size();
}

SOCKET CServerSocketList::GetSocket(int iIndex)
{
	if (iIndex >= _listSocket.size() || iIndex < 0)
		return 0;
	return _listSocket[iIndex];
}

bool CServerSocketList::SetUsername(int iIndex, char* iUsername)
{
	if (iIndex < 0 || iIndex >= _listUser.size())
		return false;
	if (_listUser[iIndex] == NULL)
		_listUser[iIndex] = new char[20];
	strcpy(_listUser[iIndex],iUsername);
	return true;
}

bool CServerSocketList::GetUserList(char* iOutString)
{
	if (_listSocket.size() == 0)
	{
		iOutString[0] = 0;
		return false;
	}
	iOutString[0] = 0;
	for (int i = 0;i<_listUser.size();i++)
	{
		strcat(iOutString,_listUser[i]);
		strcat(iOutString,"!");
	}
	iOutString[strlen(iOutString)-1] = 0;
	return true;
}
void CServerSocketList::Pop(int iIndex)
{
	if (iIndex >= _listSocket.size() || iIndex <0)
		return;
	_listSocket.erase(_listSocket.begin() + iIndex);
	_listUser.erase(_listUser.begin() + iIndex);
}